package com.wenzil.gulimall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.wenzil.gulimall.order.entity.OrderEntity;
import com.wenzil.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenzil.common.utils.PageUtils;
import com.wenzil.common.utils.Query;

import com.wenzil.gulimall.order.dao.OrderItemDao;
import com.wenzil.gulimall.order.entity.OrderItemEntity;
import com.wenzil.gulimall.order.service.OrderItemService;


//@RabbitListener(queues = "hello-java-queue")
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

//    @RabbitListener(queues = "hello-java-queue")
//    @RabbitHandler
    public void receiveMessage(Message message, OrderReturnReasonEntity content, Channel channel) {
        System.out.println("接收到消息...：" + message + "==>内容：" + content);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag=" + deliveryTag);
        // 签收货物，非批量模式
        try {
            if (deliveryTag % 2 == 0) {  // 收获
                channel.basicAck(deliveryTag, false);
                System.out.println("签收了货物" + deliveryTag);
            } else {    // 退货
                channel.basicNack(deliveryTag, false, false);
                System.out.println("没有签收货物" + deliveryTag);
            }
        } catch (IOException e) {
            // 网络中断
        }
    }

//    @RabbitHandler
    public void receiveMessage2(OrderEntity content) {
        System.out.println("接收到消息==>内容：" + content);
    }
}
